//
//  PPPicturesView.m
//  PropertyManage
//
//  Created by Dreamsoul on 15-1-16.
//  Copyright (c) 2015年 com.lab603.mc. All rights reserved.
//

#import "PPPicturesView.h"
#import "PPPictureView.h"
#import "MJPhotoBrowser.h"
#import "PPPicture.h"
#import "MJPhoto.h"
#define PPPictureW 75
#define PPPictureH 75
#define PPPictureMargin 10
@implementation PPPicturesView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        for(int i = 0;i<9;i++){
            PPPictureView *picture = [[PPPictureView alloc] init];
            picture.userInteractionEnabled = YES;
            picture.tag = i;
            [picture addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoDidTap:)]];
            [self addSubview:picture];
        }
    }
    return self;
}

- (void)photoDidTap:(UITapGestureRecognizer *)recognizer{
    //IWLog(@"----%ld---",(long)recognizer.view.tag);
    
    NSMutableArray *midPhotos = [NSMutableArray arrayWithCapacity:self.picturesArray.count];
    for (int i = 0 ; i<self.picturesArray.count; i++) {
        MJPhoto *newPhoto = [[MJPhoto alloc] init];
        //原图片
        newPhoto.srcImageView = self.subviews[i];
        PPPicture *picture = self.picturesArray[i];
        NSString *newPictureString = picture.picture;
        newPhoto.url = [NSURL URLWithString:newPictureString];
        [midPhotos addObject:newPhoto];
    }
    //创建相册浏览器
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = recognizer.view.tag;
    browser.photos = midPhotos;
    [browser show];
    
}
-(void)setPicturesArray:(NSArray *)picturesArray{
    _picturesArray = picturesArray;
    for (int i = 0 ; i<self.subviews.count; i++) {
        PPPictureView *pictureView = self.subviews[i];
        if (i<picturesArray.count) {
            pictureView.hidden = NO;
            pictureView.picture = self.picturesArray[i];
            // 设置子控件的frame
            int maxColumns = (picturesArray.count == 4) ? 2 : 3;
            int col = i % maxColumns;
            int row = i / maxColumns;
            CGFloat pictureX = col * (PPPictureW + PPPictureMargin);
            CGFloat pictureY = row * (PPPictureH + PPPictureMargin);
            pictureView.frame = CGRectMake(pictureX, pictureY, PPPictureW, PPPictureH);
            
            // Aspect : 按照图片的原来宽高比进行缩
            // UIViewContentModeScaleAspectFit : 按照图片的原来宽高比进行缩放(一定要看到整张图片)
            // UIViewContentModeScaleAspectFill :  按照图片的原来宽高比进行缩放(只能图片最中间的内容)
            // UIViewContentModeScaleToFill : 直接拉伸图片至填充整个imageView
            
            if (picturesArray.count == 1) {
                pictureView.contentMode = UIViewContentModeScaleToFill;
                pictureView.clipsToBounds = NO;
            } else {
                pictureView.contentMode = UIViewContentModeScaleAspectFill;
                pictureView.clipsToBounds = YES;
            }
            
        }else{
            pictureView.hidden=YES;
        }
    }

}

+ (CGSize)picturesViewCount:(int)count{
    // 一行最多有3列
    int maxColumns = (count == 4) ? 2 : 3;
    
    //  总行数
    int rows = (count + maxColumns - 1) / maxColumns;
    
    // 高度
    CGFloat photosH = rows * PPPictureH + (rows - 1) * PPPictureMargin;
    
    // 总列数
    int cols = (count >= maxColumns) ? maxColumns : count;
    
    // 宽度
    CGFloat photosW = cols * PPPictureW + (cols - 1) * PPPictureMargin;
    
    return CGSizeMake(photosW, photosH);
}

@end
